const Koa = require('koa');
const app = new Koa();
const path = require('path');

const views = require('koa-views');
const json = require('koa-json');
const bodyparser = require('koa-bodyparser');
const _global = require('./lib/global.lib');
const router = require('./lib/router.lib');
const logger = require('./lib/logger.lib');
const s = require('./lib/database.lib');
const interceptor = require('./lib/interceptor.lib');

app.keys = ['some secret hurr'];

app.use(json());
app.use(bodyparser({ extendTypes: ['json', 'form', 'text'] }));
app.use(views(path.join(__dirname + '/views'), { map: { html: "ejs" } }));
app.use(require('koa-static')(path.join(__dirname + '/public')));


app.use(interceptor.error);
app.use(router.routes(), router.allowedMethods());
app.use(interceptor.notFound);

app.on('error', (err, ctx) => {
    console.error('server error', err, ctx)
})

module.exports = app;